﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common.ViewModel/PagedResult 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       PagedResult
* 创建时间：  2025/5/22 14:37:00
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.Common
{

    /// <summary>
    /// 分页模型，限制分页大小
    /// </summary>
    public class PageResult : IPageResult
    {
        private int _pageSize = 10;
        private int _pageNo = 1;
        private int _recordCount = 0;

        public static PageResult Default()
        {
            return new PageResult()
            {
                PageNo = 1,
                PageSize = 10
            };
        } 
        public virtual int PageNo
        {
            get
            {
                return this._pageNo;
            }
            set
            {
                if (value <= 0)
                    this._pageNo = 1;
                else
                    this._pageNo = value;
            }
        }
        public virtual int PageSize
        {
            get
            {
                return this._pageSize;
            }
            set
            {
                if (value > 1000)
                    this._pageSize = 1000;
                else if (value <= 0)
                    this._pageSize = 10;
                else
                    this._pageSize = value;
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public virtual int PageCount { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public virtual int RecordCount
        {
            get { return _recordCount; }
            set
            {
                this._recordCount = value;
                this.PageSize = this.PageSize <= 0 ? 10 : this.PageSize;
                this.PageCount = this.RecordCount % this.PageSize == 0 ? this.RecordCount / this.PageSize : (this.RecordCount / this.PageSize + 1);
                this.PageNo = this.PageNo > this.PageCount ? this.PageCount : this.PageNo;
                this.PageNo = this.PageNo < 1 ? 1 : this.PageNo;
            }
        }


    }
    /// <summary>
    /// 分页模型，限制分页大小
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageResult<T> : PageResult,IPageResult
    {
        private int _pageSize = 10;
        private int _pageNo = 1;
        private int _recordCount = 0;

        public static PageResult<T> Default()
        {
            return new PageResult<T>()
            {
                PageNo = 1,
                PageSize = 10
            };
        }
        public List<T> Data { get; set; }
        public virtual int PageNo
        {
            get
            {
                return this._pageNo;
            }
            set
            {
                if (value <= 0)
                    this._pageNo = 1;
                else
                    this._pageNo = value;
            }
        }
        public virtual int PageSize
        {
            get
            {
                return this._pageSize;
            }
            set
            {
                if (value > 1000)
                    this._pageSize = 1000;
                else if (value <= 0)
                    this._pageSize = 10;
                else
                    this._pageSize = value;
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public virtual int PageCount { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public virtual int RecordCount
        {
            get { return _recordCount; }
            set
            {
                this._recordCount = value;
                this.PageSize = this.PageSize <= 0 ? 10 : this.PageSize;
                this.PageCount = this.RecordCount % this.PageSize == 0 ? this.RecordCount / this.PageSize : (this.RecordCount / this.PageSize + 1);
                this.PageNo = this.PageNo > this.PageCount ? this.PageCount : this.PageNo;
                this.PageNo = this.PageNo < 1 ? 1 : this.PageNo;
            }
        }


    }
    public interface IPageResult
    {
        int PageNo { get; set; }
        int PageSize { get; set; }
        int PageCount { get; set; }
        int RecordCount { get; set; }
    }
}
